/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.surfmanager.dao;

import com.surfmanager.model.User;
import com.surfmanager.util.PasswordEncrypt;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author eamiker
 */
public class LoginDAO {
    
    public String addUser(User user){
        SessionFactory sf;
        Session sesion;
        Transaction tx=null;
        try{
            sf = HibernateUtil.getSessionFactory();
            sesion = sf.openSession();
            tx=sesion.beginTransaction();
            user.setPassword(PasswordEncrypt.encrypt(user.getPassword(), "SHA", "UTF-8"));
            sesion.save(user);
            tx.commit();
            sesion.close();
            return "ok";
        }catch(Exception ex){
            tx.rollback();
            throw new RuntimeException("error al addUser");
            
        }
    }
    
        public String getUser(int id){
       
          SessionFactory  sf = HibernateUtil.getSessionFactory();
           Session sesion = sf.openSession();
           //Transaction tx=sesion.beginTransaction();
            User user = (User)sesion.get(User.class, id);
            sesion.close();
            if(user!=null){
                return user.toString();
            }else{
                return "user inexitente";
        }
        }
            
            public List<User> getALLUsers(){
                 SessionFactory  sf = HibernateUtil.getSessionFactory();
           Session sesion = sf.openSession();
           Query query = sesion.createQuery("from User");
           List<User> users = query.list();
           sesion.close();
           return users;
            }
    
     public int addUserWithId(User user){
        SessionFactory sf;
        Session sesion;
        Transaction tx=null;
        try{
            sf = HibernateUtil.getSessionFactory();
            sesion = sf.openSession();
            tx=sesion.beginTransaction();
            sesion.save(user);
            tx.commit();
            sesion.close();
            return user.getId();
        }catch(Exception ex){
            tx.rollback();
            throw new RuntimeException("error al addUser");
            
        }
    }
     
     public void remove(Integer id) {
		SessionFactory sf;
        Session sesion;
        Transaction tx=null;
        try{
            sf = HibernateUtil.getSessionFactory();
            sesion = sf.openSession();
            tx=sesion.beginTransaction();
            User user = (User)sesion.get(User.class, id);		
	    sesion.delete(user);
            tx.commit();
            sesion.close();
           
        }catch(Exception ex){
            tx.rollback();
            throw new RuntimeException("error al addUser");
            
        }
		
	}
    
}
